package com.vladmr.sleepytimepicker;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class NotificationsDialog extends Activity {
	//private static final String TAG = "sleepytimepicker";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notifications_dialog);

		Bundle extras = getIntent().getExtras();
		String str_reminder = extras.getString("reminder");
		TextView tv_reminder = (TextView) findViewById(R.id.tv_reminder);
		tv_reminder.setText(str_reminder);

		if (android.os.Build.VERSION.SDK_INT >= 14) {
			Switch switch_reminder = (Switch) findViewById(R.id.switch_reminder);
			switch_reminder.setChecked(true);

			switch_reminder
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (!isChecked) {
								SwitchUnchecked();
							}
						}
					});
		} else {
			ToggleButton btn_switch = (ToggleButton) findViewById(R.id.btn_switch);
			btn_switch.setChecked(true);

			btn_switch
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (!isChecked) {
								SwitchUnchecked();
							}
						}
					});
		}

	}

	private void SwitchUnchecked() {
		PendingIntent sender = ChooseFallasleepActivity
				.getSyncPendingIntent(getApplicationContext());
		AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
		am.cancel(sender);
		Toast.makeText(getApplicationContext(), "reminder OFF",
				Toast.LENGTH_SHORT).show();

		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		mNotificationManager.cancelAll();
		finish();
	}
}
